﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;
using System.Collections.Generic;

public partial class AdminX_Cruise_CruiseMan : System.Web.UI.UserControl
{
    private int nPageCount = 0;
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        this.initDataPaging();
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            this.ShowMess();
            this.BindDataToLanguageDropDownList();
            this.BindDataToGrid(1);
        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
        btnUpdate.Text = MultipleLanguage.BTN_Update[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
    }
    private void BindDataToLanguageDropDownList()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }
    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nItemView = 0;
        int nItemID = 0;
        DataTable dtbCruise = new DataTable();
        Cruise objCruise = new Cruise();
        dtbCruise = objCruise.Search(nItemView, nItemID, 0, 0, 0, -1, CurrentPageIndex, Config.PAGESIZE, ref nPageCount);
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];

        Pager1.ItemCount = nPageCount;
        VisiblePaging(nPageCount);

        dtgCruise.DataSource = dtbCruise;
        dtgCruise.DataBind();
        Pager1.ItemCount = nPageCount;

        if (nPageCount > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;
    }
    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }
    private void ShowMess()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_CRUISE_ADD;
        Response.Redirect(sAdd);
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        // Update  data
        int nCount = dtgCruise.Items.Count;

        for (int i = 0; i < nCount; i++)
        {
            TextBox txtPriority = (TextBox)dtgCruise.Items[i].FindControl("txtPriority");
            Label lblID = (Label)dtgCruise.Items[i].FindControl("lblCruiseID");

            int nCruiseID = ConvertData.ConvertToInt(lblID.Text);
            int nPriority = ConvertData.ConvertToInt(txtPriority.Text);

            Cruise objCruise = new Cruise();
            int nResult = objCruise.UpdatePriority(nCruiseID, nPriority);

            if (nResult > 0) { Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage - 1]); }
            else { Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage - 1]); }
        }
        this.BindDataToGrid(1);
    }
    protected void dtgCruise_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Cruise objCruise = new Cruise();
        switch (strCommand)
        {
            case "Delete":
                objCruise.LoadById(nID);
                Support.DeleteFile("Cruise", ConvertData.ConvertToString(objCruise.Data.ImageDetail1));
                Support.DeleteFile("Cruise", ConvertData.ConvertToString(objCruise.Data.ImageDetail2));
                Support.DeleteFile("Cruise", ConvertData.ConvertToString(objCruise.Data.ImageDetail3));
                Support.DeleteFile("Cruise", ConvertData.ConvertToString(objCruise.Data.ImageDetail4));
                Support.DeleteFile("Cruise", ConvertData.ConvertToString(objCruise.Data.ImageDetail5));

                int nDelete = objCruise.DeleteById(nID);
                if (nDelete > 0)
                {
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage - 1]);
                    BindDataToGrid(Pager1.CurrentIndex);
                }
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage - 1]);
                break;
            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_CRUISE_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            case "Active":
                int nActive = objCruise.UpdateStatus(nID, EnumeType.INACTIVE);
                if (nActive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Inactive":
                int nInactive = objCruise.UpdateStatus(nID, EnumeType.ACTIVE);
                if (nInactive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;
        }
    }
    protected void dtgCruise_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nCurrentLangID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            //Label lblStartDestination = (Label)e.Item.FindControl("lblStartDestination");
            //Label lblArrivedDestination = (Label)e.Item.FindControl("lblArrivedDestination");
            //Label lblCategory = (Label)e.Item.FindControl("lblCategory");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            TextBox txtPriority = (TextBox)e.Item.FindControl("txtPriority");
            Label lblCruiseID = (Label)e.Item.FindControl("lblCruiseID");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CruiseID"));
            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            string nPriority = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Priority"));
            lblCruiseID.Text = ConvertData.ConvertToString(nID);
            txtPriority.Text = nPriority;

            switch (nCurrentLangID)
            {
                case 1: // Vietnam

                    lblTitle.Text = ConvertData.TruncateString(ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title1")), 80) + Config.DOT;
                    
                    //lblStartDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StartDestinationTitle1"));
                    //lblArrivedDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ArrivedDestinationTitle1"));

                    break;
                case 2: //English
                    lblTitle.Text = ConvertData.TruncateString(ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title2")), 80) + Config.DOT;

                    //lblStartDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StartDestinationTitle2"));
                    //lblArrivedDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ArrivedDestinationTitle2"));

                    break;
                case 3:
                    lblTitle.Text = ConvertData.TruncateString(ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title3")), 80) + Config.DOT;

                    //lblStartDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StartDestinationTitle3"));
                    //lblArrivedDestination.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ArrivedDestinationTitle3"));

                    break;
            }

            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }

            lnkActive.Text = MultipleLanguage.STATUS_ACTIVE[nCurrentLangID - 1];
            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CruiseID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage - 1]);
            lnkInactive.Text = MultipleLanguage.STATUS_INACTIVE[nCurrentLangID - 1];
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CruiseID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLanguage - 1]);

            lnkEdit.Text = MultipleLanguage.BTN_EDIT[nDefaultLanguage - 1];
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CruiseID"));

            lnkDelete.Text = MultipleLanguage.BTN_DELETE[nDefaultLanguage - 1];
            lnkDelete.Enabled = true;
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CruiseID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage - 1]);
        }
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }
}